package com.zhongwang.mms.module.buy.listner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.mms.common.ImportCallBack;
import com.zhongwang.mms.gen.entity.TDictionary;
import com.zhongwang.mms.gen.entity.TEmploy;
import com.zhongwang.mms.module.buy.dao.BuyPlanDao;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.*;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.CodeUtil;
import com.zhongwang.mms.util.ShiroUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zhongwang.mms.util.BeanUtil.copy;

/**
 * @Description: 采购计划修改行号导入 listener
 * @Author: China.wsg
 * @Date: 2020/01/09 16:38
 */
public class ImportShoppingPlanRowNoListener extends AnalysisEventListener<ImportShoppingPlanRowNoModel> {

    private BuyPlanDao buyPlanDao;
    private ImportCallBack callBack;
    private List<ImportShoppingPlanRowNoModel> exceptionImportShoppingPlanRowNoModels = new ArrayList<>();
    private List<ImportShoppingPlanRowNoModel> importShoppingPlanRowNoModels = new ArrayList<>();
    private List<Integer> exceptionList = new ArrayList<>();
    private LogService logService;
    private int i = 2;

    public ImportShoppingPlanRowNoListener(BuyPlanDao buyPlanDao,
                                           ImportCallBack callBack,
                                           LogService logService) {
        this.buyPlanDao = buyPlanDao;
        this.callBack = callBack;
        this.logService = logService;
    }

    @Override
    public void invoke(ImportShoppingPlanRowNoModel data, AnalysisContext context) {
        if (data.getSppId() != null && !data.getSppId().trim().equals("")) {
            int sppId=0;
            if(data.getSppId().contains(".")){
                data.setSppId(data.getSppId().substring(0,data.getSppId().indexOf(".")));
            }
            try{
                sppId = Integer.parseInt(data.getSppId());
            }
            catch (Exception e){
                e.printStackTrace();
                ErrorModel errorModel = new ErrorModel();
                errorModel.setErrorInfo("ID不是纯数字！");
                errorModel.setRowIndex(exceptionImportShoppingPlanRowNoModels.size()+1);
                errorModel.setColIndex(1);
                errorModel.setCorrectInfo("ID必须为纯数字！");
                data.setErrorModel(errorModel);
                exceptionImportShoppingPlanRowNoModels.add(data);
            }
            if(sppId!=0){
                ShoppingPlanModel shoppingPlanModel = buyPlanDao.getShoppingPlanByPrimaryKey(sppId);
                if(null != shoppingPlanModel){
                    if(data.getSppRowNo().contains(".")){
                        data.setSppRowNo(data.getSppRowNo().substring(0,data.getSppRowNo().indexOf(".")));
                    }
                    try{
                       Integer sppRowNo = Integer.parseInt(data.getSppRowNo());
                    }
                    catch (Exception e){
                        e.printStackTrace();
                        ErrorModel errorModel = new ErrorModel();
                        errorModel.setErrorInfo("行号不是纯数字！");
                        errorModel.setRowIndex(exceptionImportShoppingPlanRowNoModels.size()+1);
                        errorModel.setColIndex(6);
                        errorModel.setCorrectInfo("行号必须为纯数字！");
                        data.setErrorModel(errorModel);
                        exceptionImportShoppingPlanRowNoModels.add(data);
                    }
                    importShoppingPlanRowNoModels.add(data);
                }else {
                    ErrorModel errorModel = new ErrorModel();
                    errorModel.setErrorInfo("ID不存在");
                    errorModel.setRowIndex(exceptionImportShoppingPlanRowNoModels.size()+1);
                    errorModel.setColIndex(1);
                    errorModel.setCorrectInfo("请勿改动ID！");
                    data.setErrorModel(errorModel);
                    exceptionImportShoppingPlanRowNoModels.add(data);
                }
            }
        } else {
            ErrorModel errorModel = new ErrorModel();
            errorModel.setErrorInfo("ID不存在");
            errorModel.setRowIndex(exceptionImportShoppingPlanRowNoModels.size()+1);
            errorModel.setColIndex(1);
            errorModel.setCorrectInfo("请勿改动ID！");
            data.setErrorModel(errorModel);
            exceptionImportShoppingPlanRowNoModels.add(data);
        }
        i++;
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (exceptionImportShoppingPlanRowNoModels.size() > 0) {
            callBack.onWarning(exceptionImportShoppingPlanRowNoModels);
        } else {
            for (ImportShoppingPlanRowNoModel model: importShoppingPlanRowNoModels) {

                ShoppingPlanModel before = buyPlanDao.getShoppingPlanByPrimaryKey(Integer.parseInt(model.getSppId()));
                if(null == before.getSppRowNo() || before.getSppRowNo().equals("")){
                    before.setSppRowNo("");
                }
                buyPlanDao.updateShoppingPlanSppRowNoByPrimaryKey(model);
                ShoppingPlanModel alter = buyPlanDao.getShoppingPlanByPrimaryKey(Integer.parseInt(model.getSppId()));
                if(null == alter.getSppRowNo() || alter.getSppRowNo().equals("")){
                    alter.setSppRowNo("");
                }
                logService.saveLog("【采购】采购进度编辑-批量导入行号","采购物料描述为："+before.getMatDesc()+"的修改前行号是："+before.getSppRowNo()+"。修改后的行号是："+alter.getSppRowNo()+"。");
            }
            callBack.onSuccess();
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exceptionList.add(i);
        i++;
    }

}